import { reactive, computed, toRefs, ref } from 'vue';
import { getModels } from '@/api/warehouse';
import detailData from '@/hooks/dictDetail';
import typeSeries from '@/hooks/typeSeries';
import { Pagination, PaginationQuery } from '@/types/table';
import { TableState, CheckAble } from '@/types/table';

const tableQuery: PaginationQuery = reactive({
    page: 0,
    size: 9,
    sort: 'id,desc',
});

const typeQuery: PaginationQuery = reactive({
    page: 0,
    size: 9,
    sort: 'id,desc',
    type: 0,
    pCode: '',
});
const typeQuery1: PaginationQuery = reactive({
    page: 0,
    size: 9,
    sort: 'id,desc',
    type: 0,
    pCode: '',
});
const dictNameQuery: PaginationQuery = reactive({
    dictName: '',
});

// 角色信息
const state: TableState & CheckAble = reactive({
    dataSource: [],
    selectedRow: {},
    selectColor: [],
    selectRatio: [],
    selectDimension: [],
    selectFirm: [],
    dataType: [],

    selectedRowKeys: [],
    visible: false,
    loading: false,
    pagination: {
        total: 0,
        current: computed(() => tableQuery.page + 1),
    },
});
const allSeriesList: any = ref([]);
// 获取table数据
const getModelData = async (tableQuery: PaginationQuery) => {
    state.loading = true;
    const { content, totalElements } = await getModels(tableQuery);
    state.loading = false;
    state.dataSource = content;
    state.pagination.total = totalElements;
};
// 设置table row key
const setRowKey = (record: { [x: string]: any }) => record.id;

// 页码改变
const handleTableChange = (pagination: Pagination) => {
    const { current } = pagination;
    tableQuery.page = current - 1;
    getModelData(tableQuery);
};

// 显示新增型号弹窗
const handleAddModel = () => {
    state.selectedRow = {};
    state.visible = true;
};

// 获取字典详情
const getDictDetailData = async (dictNameQuery: PaginationQuery) => {
    const { list, total } = await detailData(dictNameQuery);
    state.selectColor = list.color;
    state.selectRatio = list.ratio;
    state.selectDimension = list.size;
    state.selectFirm = list.firm;
};

//获取所有的设备类型
const getTypeData = async (typeQuery1: PaginationQuery) => {
    state.loading = true;
    const { list, total } = await typeSeries(typeQuery);
    state.loading = false;
    state.dataType = list;
    state.pagination.total = total;
};

//根据类型获取所有的系列
const allSeriesData = async () => {
    const { list, total } = await typeSeries({
        page: 0,
        size: 999,
        sort: 'id,desc',
        type: 1,
    });
    allSeriesList.value = list;
};

// 编辑型号
const handleEdit = (dict: { [x: string]: any }) => {
    console.log(dict);
    state.selectedRow = dict;
    state.visible = true;
};

// 取消弹窗
const handleCancle = () => {
    state.visible = false;
};

export default {
    tableQuery,
    typeQuery,
    typeQuery1,
    dictNameQuery,
    allSeriesList,
    getDictDetailData,
    handleTableChange,
    handleAddModel,
    handleEdit,
    getModelData,
    getTypeData,

    allSeriesData,
    setRowKey,
    ...toRefs(state),
    handleCancle,
};
